package com.bishe.controller;


import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bishe.model.LoginModel;
import com.bishe.pojo.FileInfo;
import com.bishe.pojo.Teacher;
import com.bishe.query.FileQuery;
import com.bishe.query.TeacherQuery;
import com.bishe.service.IFileService;
import com.bishe.service.TeacherService;
import com.bishe.util.EcoBootException;
import com.bishe.util.JobUtils;
import com.bishe.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lwx
 * @since 2022-04-14
 */
@RestController
@CrossOrigin
@Api(tags = "教师控制器")
@RequestMapping("/teacher")
public class TeacherController {

    @Resource
    private TeacherService teacherService;

    @ApiOperation("教师登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginModel loginModel){
        LoginModel login = teacherService.login(loginModel);

        if (login!=null){
            return Result.ok("ok",login);
        }
        return Result.error("用户名密码错误!");
    }

    @ApiOperation("教师列表")
    @GetMapping("/toPage")
    public Result toPage(TeacherQuery query) {
        IPage<Teacher> page = teacherService.toPage(query);
        return Result.ok(page);
    }

    @ApiOperation("我的文件")
    @GetMapping("/myFiles")
    public Result myFiles(FileQuery query) {
        IPage<FileInfo> page = teacherService.myFiles(query);
        return Result.ok(page);
    }

    @ApiOperation("添加或修改")
    @PostMapping("/addOrUpdate")
    public Result addOrUpdate(@RequestBody Teacher teacher) {

        if (StrUtil.isAllEmpty(teacher.getName(),teacher.getTeacherNo())){
            throw new EcoBootException("信息不能为空");
        }
        teacherService.addOrUpdate(teacher);
        return Result.ok("ok");
    }

    @ApiOperation("删除学生")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable String id){
        if (StrUtil.isEmpty(id)){
            throw new EcoBootException("id不能为空");
        }
        teacherService.removeById(id);
        return Result.ok();
    }


}

